using System;
using System.Linq;
using System.Collections.Generic;

namespace Ants {

	class MyBot : Bot {

		// DoTurn is run once per turn
		public override void DoTurn (GameState state) {
			List<RegisteredAnt> processedAnts = new List<RegisteredAnt>();
			// loop through all my ants and try to give them orders
			foreach (Ant ant in state.MyAnts) {
                RegisteredAnt regAnt = GetAntFromCollection(state.RegisteredAnts, ant);
				if (regAnt == null)
				{
					regAnt = new RegisteredAnt(ant);
				}

                if (regAnt.IsNewAnt)
                {
					regAnt.SetDirection(state, Direction.South);
					regAnt.IsNewAnt = false; ;
                }
				regAnt.SetDirection(state, TryDirection(state, regAnt, regAnt.Direction));
				processedAnts.Add(regAnt);
                IssueOrder(ant, regAnt.Direction);
				// check if we have time left to calculate more orders
				if (state.TimeRemaining < 10) break;
			}
			state.RegisteredAnts = processedAnts;
		}
		
		public static void Main (string[] args) {
//#if DEBUG
//            System.Diagnostics.Debugger.Launch();
//            while (!System.Diagnostics.Debugger.IsAttached)
//            { }
//#endif
			new Ants().PlayGame(new MyBot());
        }

        #region private methods
        private Direction TryDirection(GameState state, RegisteredAnt ant, Direction direction)
        {
            Location loc = state.GetDestination(ant, direction);
            switch (state.GetTile(loc))
            {
                case Tile.Ant:
                    return TryDirection(state, ant, Reverse(direction));
                case Tile.Dead:
                case Tile.Food:
                case Tile.Hill:
                case Tile.Land:
                case Tile.Unseen:
                    return direction;
                case Tile.Water:
                    return TryDirection(state, ant, QuarterTurn(direction));

            }
            return Direction.South;
        }

        private Direction Reverse(Direction direction)
        {
            switch (direction)
            {
                case Direction.North:
                    return Direction.South;
                case Direction.East:
                    return Direction.West;
                case Direction.South:
                    return Direction.North;
                case Direction.West:
                    return Direction.East;
            }
            return Direction.South;
        }

        private Direction QuarterTurn(Direction direction)
        {
            switch (direction)
            {
                case Direction.North:
                    return Direction.East;
                case Direction.East:
                    return Direction.South;
                case Direction.South:
                    return Direction.West;
                case Direction.West:
                    return Direction.North;
            }
            return Direction.South;
        }

        private RegisteredAnt GetAntFromCollection(List<RegisteredAnt> list, Ant ant)
        {
            foreach (RegisteredAnt regant in list)
            {
                if (regant.PredictedCol == ant.Col && regant.PredictedRow == ant.Row && regant.Team == ant.Team)
                {
					regant.ChangeLocation(regant.PredictedRow, regant.PredictedCol);
                    return regant;
                }
            }
            return null;
        }
        #endregion
    }
	
}